var express = require('express');
var axios = require('axios');
var router = express.Router();

/**
 * @api {get} /admin/addteacher 渲染页面
 * @apiName 渲染页面
 * @apiGroup Teacher
 *
 *
 * @apiSuccess {String} firstname Firstname of the User.
 * @apiSuccess {String} lastname  Lastname of the User.
 */
router.get('/',(req, res, next) => {
    if (req.session.login) {
        next()
    } else {
        res.send({ err: -999, msg: '请先登录' })
    }
}, function (req, res, next) {
    var config = {
        method: 'post',
        baseURL: 'http://localhost:9023',
        url: '/Teacher/getAllKind',
        params: {
            Kind: 'b'
        },
    }
    axios(config)
        .then(function (response) {
            console.log(response.data);
            if (response.data.code == 0) {
                console.log("获取教师信息成功")
                res.render('admin/addTeacher', {
                    list: response.data.data
                });
            } else {
                // console.log(response);
                res.send({ err: -1, msg: "获取教师信息失败" })
                console.log("获取教师信息失败")
            }
        })
        .catch(function (error) {
            console.log(error);
            res.send({ err: -1, msg: "服务器错误" })
            console.log("服务器错误")
        });

})

/**
 * @api {post} /admin/addteacher/upload 教师信息导入
 * @apiName 发布页面
 * @apiGroup Teacher
 *
 * @apiParam {file} files 教师信息表.
 *
 * @apiSuccess {String} firstname Firstname of the User.
 * @apiSuccess {String} lastname  Lastname of the User.
 */
router.post('/upload',(req, res, next) => {
    if (req.session.login) {
        next()
    } else {
        res.send({ err: -999, msg: '请先登录' })
    }
}, function (req, res, next) {
    let files = req.body.files
    console.log(files)
    let param = new URLSearchParams()
    var config = {
        method: 'post',
        baseURL: 'http://localhost:9023',
        url: '/Teacher/readExcel',
        data: param,
    }
    axios(config)
        .then(function (response) {
            console.log(response.data);
            if (response.data.code == 0) {
                console.log("上传成功")
                res.send({ err: 0, msg: "上传成功", data: response.data })
            } else {
                // console.log(response);
                res.send({ err: -1, msg: "上传失败" })
                console.log("上传失败")
            }
        })
        .catch(function (error) {
            console.log(error);
            res.send({ err: -1, msg: "服务器错误" })
            console.log("服务器错误")
        });
})

/**
 * @api {post} /admin/addteacher/delete 教师信息删除
 * @apiName delete
 * @apiGroup Teacher
 *
 * @apiParam {string} teacherId 教师id.
 *
 * @apiSuccess {String} firstname Firstname of the User.
 * @apiSuccess {String} lastname  Lastname of the User.
 */
router.post('/delete',(req, res, next) => {
    if (req.session.login) {
        next()
    } else {
        res.send({ err: -999, msg: '请先登录' })
    }
}, function (req, res, next) {
    let { teacherId } = req.body
    console.log(teacherId)
    var config = {
        method: 'post',
        baseURL: 'http://localhost:9023',
        url: '/Teacher/deleteTeacherById',
        params: {
            teacherId: teacherId
        },
    }
    axios(config)
        .then(function (response) {
            console.log(response.data);
            if (response.data.code == 0) {
                console.log("删除成功")
                res.send({ err: 0, msg: "删除成功", data: response.data })
            } else {
                // console.log(response);
                res.send({ err: -1, msg: "删除失败" })
                console.log("删除失败")
            }
        })
        .catch(function (error) {
            console.log(error);
            res.send({ err: -1, msg: "服务器错误" })
            console.log("服务器错误，删除失败")
        });
})

module.exports = router;
